public with sharing class AccountClassificationTriggerHandler {
    
    public void OnAfterAccountPimsId(List<Account> accountList){
    Set<String> partnerid=new Set<String>();
    map<String,Id> accountmap=new map<String,Id> ();
    if(accountList!=null && accountList.size()>0){
        
    for(Account acc:accountList){
        partnerid.add(acc.Partner_ID__c);
        accountmap.put(acc.Partner_ID__c,acc.Id);
    }
         
            List<PIMS_Reserved_Id__c> pimsid=[select Id,Account__c,PIMS_Id__c,Used__c from PIMS_Reserved_Id__c where PIMS_Id__c IN: partnerid];
            List<PIMS_Reserved_Id__c> updatepimsidList=new List<PIMS_Reserved_Id__c>();
             if(pimsid != null){
                 for(PIMS_Reserved_Id__c pims : pimsid){
                    pims.Account__c=accountmap.get(pims.PIMS_Id__c);
                    pims.Used__c=true;
                    updatepimsidList.add(pims);
                 }
             }
             
           if(!updatepimsidList.isEmpty()){
           try{ 
             update updatepimsidList;
             }catch(Exception e){
            system.debug('# Exception while updating : '+e);
        }
    }
    
    }
}

public void OnBeforeAccountPimsId(List<Account> account){
    
     //map<String,Id> accountmap=new map<String,Id> ();
     List<String> pimsid=new List<String>();
     List<PIMS_Reserved_Id__c> pimsidList=[select Id,PIMS_Id__c from PIMS_Reserved_Id__c where Used__c=false];
    
    for(PIMS_Reserved_Id__c pims:pimsidList)
    {
       pimsid.add(pims.PIMS_Id__c);
    }
    Integer i=0;
    if(account !=null && account.size()>0){
         for(Account acc:account){
            
            if(acc.Partner_ID__c==null && pimsid[i]!=null){
                 acc.Partner_ID__c=pimsid[i];
                 i++;
              }
            
        }
    }
}

public void OnbeforeClassificationDateTrigger(List<Classification__c> classification){
    
    List<Classification__c> updateClassificationList=new List<Classification__c>();
    
    if(classification != null && classification.size()>0){
        List<classificationValue__c> classificationValueList=new List<classificationValue__c>();
        List<Classification__c> classificationOld=new List<Classification__c>();
        
        List<String> ClassificationValue=new List<String>();
        
        for(Classification__c classificationNew:classification){
            ClassificationValue.add(classificationNew.Classification_Value__c);
            if(classificationNew.End_Date__c !=null){
            	classificationNew.Status__c='Inactive';
            }
            }
            try{
                 classificationValueList=[select Value_in_PIMS__c,Value_in_SFDC__c from classificationValue__c where Value_in_SFDC__c IN: ClassificationValue];
                 classificationOld=[select Id,Status__c,End_Date__c,Classification_Code__c,Affiliate_Account__c from Classification__c where Id!=null ];
            }catch(Exception e){}
        for(Classification__c classificationNew:classification){
            //To insert Classification Value..
            
            if(classificationNew.Classification_Value__c==null){
                if(classificationNew.Employee_Identification_Number_EIN__c!=null){classificationNew.ClassificationValue__c=classificationNew.Employee_Identification_Number_EIN__c;}
                else{
                    if(classificationNew.Service_Area_Population__c!=null){classificationNew.ClassificationValue__c=String.valueOf(classificationNew.Service_Area_Population__c);}
                    else
                        if(classificationNew.Vacation_Months__c!=null){classificationNew.ClassificationValue__c=classificationNew.Vacation_Months__c;}
                }
            }
            else{
                 if(classificationValueList !=null){
                    for(classificationValue__c classi:classificationValueList){
                        if(classi.Value_in_SFDC__c==classificationNew.Classification_Value__c)
                        classificationNew.ClassificationValue__c=classi.Value_in_PIMS__c;
                    }
                }
            }
            
            //To auto-populate the end date of old record, when a new record is created having same code..
            
            try{
                 if(classificationOld!= null){
                    for(Classification__c c:classificationOld){
                        if(c.Classification_Code__c==classificationNew.Classification_Code__c && c.Affiliate_Account__c==classificationNew.Affiliate_Account__c && c.End_Date__c==null){
                            c.End_Date__c=classificationNew.Start_Date__c;
                            c.Status__c='Inactive';
                            updateClassificationList.add(c);
                            // update c;
                        }
                    }
                    
                 }
             }catch(Exception e){system.debug('# Exception '+e);}   
        }
        if(!updateClassificationList.isEmpty()){
        try{
        update updateClassificationList;
        }catch(Exception e1){
            system.debug('# Exception while updating : '+e1);
        }
    }
    }
}

}